package com.linkoog.devtools.metadata.parser;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.linkoog.devtools.metadata.raw_index.ClassMetadata;
import com.linkoog.devtools.metadata.raw_index.MethodMetadata;
import com.linkoog.devtools.type.Type;
import com.linkoog.devtools.type.TypeParser;
import com.linkoog.devtools.type.javadoc.JavaDoc;
import com.linkoog.devtools.type.javadoc.JavaDocParser;
import com.linkoog.devtools.type.swagger.JavaSwaggerParser;
import com.linkoog.devtools.utils.PsiElementUtil;
import com.linkoog.devtools.utils.VirtualFileUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ClassMetadataParser {


    public static ClassMetadata parseClass(PsiClass psiClass){
        LinkedHashMap<Integer, MethodMetadata> methodMetadataList = new LinkedHashMap<>();
        /*for (PsiMethod psiMethod : psiClass.getAllMethods()) {
            if (PsiMethodUtil.isObjectMethod(psiMethod)) continue;
            MethodMetadata methodMetadata = parseMethod(psiMethod);
            methodMetadataList.put(psiMethod.getTextOffset(), methodMetadata);
        }*/

        final VirtualFile virtualFile = psiClass.getContainingFile().getOriginalFile().getVirtualFile();
        final long timestamp = VirtualFileUtils.getTimestamp(virtualFile);
        String moduleFilePath = PsiElementUtil.resolveModuleFilePath(psiClass);
        ClassMetadata classMetadata = new ClassMetadata(virtualFile.getUrl(), psiClass.getTextOffset(), psiClass.getQualifiedName(),moduleFilePath);
        classMetadata.initPsiElement(psiClass.getProject(), psiClass);
        classMetadata.setJavaDoc(JavaDocParser.parse(psiClass.getDocComment()));
        classMetadata.setSwaggerComment(JavaSwaggerParser.parseClass(psiClass));
        classMetadata.setTimestamp(timestamp);
        return classMetadata;
    }


    /**
     *
     */
    public static MethodMetadata parseMethod(PsiMethod psiMethod) {
        final VirtualFile virtualFile = psiMethod.getContainingFile().getOriginalFile().getVirtualFile();
        final long timestamp = VirtualFileUtils.getTimestamp(virtualFile);
        MethodMetadata methodMetadata = new MethodMetadata(virtualFile.getUrl(), psiMethod.getTextOffset(), psiMethod.getName(),
                ()-> {
                    List<Type> paramTypes = new ArrayList<>();
                    PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
                    for (PsiParameter parameter : parameters) {
                        Type paramType = TypeParser.parse(parameter.getType());
                        paramTypes.add(paramType);
                    }
                    return paramTypes;
                },
                ()->TypeParser.parse(psiMethod.getReturnType()));
        methodMetadata.initPsiElement(psiMethod.getProject(), psiMethod);
        methodMetadata.setJavaDocSupplier(() -> JavaDocParser.parse(psiMethod.getDocComment()));
        methodMetadata.setSwaggerCommentSupplier(() -> JavaSwaggerParser.parseMethod(psiMethod));
        methodMetadata.setTimestamp(timestamp);
        return methodMetadata;
    }


}
